package com.silences.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.silences.pojo.Orders;
import com.silences.service.OrdersService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Map;


@WebServlet("/orders")
public class OrdersServlet extends HttpServlet {
    //创建对象
    OrdersService ordersService = new OrdersService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doPost(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        //乱码问题统一解决
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //通过method字段识别方法
        String method =req.getParameter("method");
        if(method.equals("getOrderss")){
            getOrderss(req,resp); //获取商品裂帛啊
        }else if(method.equals("getOrders")){
            getOrders(req,resp);//获取单个商品裂帛啊
        }else if(method.equals("addOrders")){
            addOrders(req,resp);
        }else if(method.equals("updateOrders")){
            updateOrders(req,resp);
        }else if(method.equals("deleteOrders")){
            deleteOrders(req,resp);
        }else if(method.equals("getOrdersName")){
            getOrdersName(req,resp);
        }else if(method.equals("updateOrdersName")){
            updateOrdersName(req,resp);
        }else if(method.equals("getOrdersNames")){
            getOrdersNames(req,resp);
        }
    }



    private void addOrders(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String ordersname =req.getParameter("ordersname");
        String ordersnum =req.getParameter("ordersnum");
        String ordersplace =req.getParameter("ordersplace");
        String ordersprice =req.getParameter("ordersprice");
        String avatar= req.getParameter("avatar");
        String createdate= req.getParameter("createdate");
        String oname=req.getParameter("oname");

        try {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            Orders orders = new Orders(0,ordersname,ordersnum,ordersplace,ordersprice,avatar,sf.parse(createdate),oname);
            Map<String,Object> map= ordersService.addOrderss(orders);
            resp.getWriter().write(JSON.toJSON(map).toString());
            //resp.getWriter().write(JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteDateUseDateFormat).toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    //请求转发 request。getRequestDispatcher(跳转路径).forwad(req,resp)
    //   1  可以携带参数    2  游览器里面路径没有变化
    // 重定向 response.sendirect(跳转路径）
    // 1   不可携带参数，游览器里面路径会发生改变


    private void getOrderss(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String searchname = req.getParameter("searchname");
        String pageIndex= req.getParameter("pageIndex");
        String pageSize = req.getParameter("pageSize");
        try {
            Map<String,Object> map = ordersService.getOrderss(searchname, pageIndex, pageSize);

           /* //修改部分 开始
            req.setAttribute("Orderss",map.get("data"));
            //请求转发
            req.getRequestDispatcher("/Orders2.jsp").forward(req,resp);
            //修改部分 结束*/

            resp.getWriter().write(JSON.toJSON(map).toString());
            //resp.getWriter().write(JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteDateUseDateFormat).toString());
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private void getOrders(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String id = req.getParameter("ordersid");
        try {

            Map<String,Object> map= ordersService.getOrders(Integer.parseInt(id));
           resp.getWriter().write(JSON.toJSON(map).toString());
            //resp.getWriter().write(JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteDateUseDateFormat).toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void getOrdersName(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String searchname = req.getParameter("searchname");
        String pageIndex= req.getParameter("pageIndex");
        String pageSize = req.getParameter("pageSize");
        try {
            Map<String,Object> map= ordersService.getOrdersName(searchname,pageIndex,pageSize);
            resp.getWriter().write(JSON.toJSON(map).toString());
            //resp.getWriter().write(JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteDateUseDateFormat).toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    private void getOrdersNames(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String searchname = req.getParameter("searchname");
        String oname = req.getParameter("oname");
        String pageIndex= req.getParameter("pageIndex");
        String pageSize = req.getParameter("pageSize");
        try {
            Map<String,Object> map= ordersService.getOrdersNames(searchname,oname,pageIndex,pageSize);
            resp.getWriter().write(JSON.toJSON(map).toString());
            //resp.getWriter().write(JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteDateUseDateFormat).toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    private void updateOrders(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        /*String id =req.getParameter("id");
        String Ordersname =req.getParameter("Ordersname");
        String password =req.getParameter("password");
        String avatar= req.getParameter("avatar");
        String createdate= req.getParameter("createdate");*/

        String ordersid=req.getParameter("ordersid");
        String ordersname =req.getParameter("ordersname");
        String ordersnum =req.getParameter("ordersnum");
        String ordersplace =req.getParameter("ordersplace");
        String ordersprice =req.getParameter("ordersprice");
        String avatar= req.getParameter("avatar");
        String createdate= req.getParameter("createdate");
        String oname = req.getParameter("oname");
        try {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            Orders orders = new Orders(Integer.parseInt(ordersid),ordersname,ordersnum,ordersplace,ordersprice,avatar,sf.parse(createdate),oname);
            Map<String,Object> map= ordersService.updateOrderss(orders);
            resp.getWriter().write(JSON.toJSON(map).toString());
            //resp.getWriter().write(JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteDateUseDateFormat).toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    //
    private void updateOrdersName(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        /*String id =req.getParameter("id");
        String Ordersname =req.getParameter("Ordersname");
        String password =req.getParameter("password");
        String avatar= req.getParameter("avatar");
        String createdate= req.getParameter("createdate");*/

        String oname = req.getParameter("oname");
        try {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            /*Orders orders = new Orders(Integer.parseInt(ordersid),ordersname,ordersnum,ordersplace,ordersprice,avatar,sf.parse(createdate),oname);*/
            Map<String,Object> map= ordersService.updateOrderssName(oname);
            resp.getWriter().write(JSON.toJSON(map).toString());
            //resp.getWriter().write(JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteDateUseDateFormat).toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }



    private void deleteOrders(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String ordersid = req.getParameter("ordersid");
        try {
            Map<String,Object> map= ordersService.deleteOrders(Integer.parseInt(ordersid));
            resp.getWriter().write(JSON.toJSON(map).toString());
            //resp.getWriter().write(JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteDateUseDateFormat).toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
